package com.yuke.cloud.service.smc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.omc.service.OmcOrderFeignApi;
import com.yuke.cloud.service.smc.dto.DmsStoreDto;
import com.yuke.cloud.service.smc.entity.DmsStore;
import com.yuke.cloud.service.smc.entity.Shop;
import com.yuke.cloud.service.smc.service.DmsStoreService;
import com.yuke.cloud.service.smc.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2020/07/04.
*/
@Api(value = "WEB - DmsStoreController", tags = "处理DmsStore相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/dms/store")
public class DmsStoreController extends BaseController {

    @Resource
    private DmsStoreService dmsStoreService;
    @Resource
    private ShopService shopService;
    @Resource
    private OmcOrderFeignApi omcOrderFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "dmsStore", value = "该数据记录的POJO") @RequestBody DmsStore dmsStore) {
        if (dmsStore != null && dmsStore.getTelephone() != null){
            //查找有没有shopId已经注册了
            Shop shop = new Shop();
            shop.setTelephone(dmsStore.getTelephone());
            List<Shop> shopList = shopService.select(shop);
            if (shopList != null && shopList.size() > 0){
                dmsStore.setShopId(shopList.get(0).getShopId());
            }
            if (dmsStoreService.save(dmsStore) > 0){
                WrapMapper.ok();
            }
        }
        return WrapMapper.error("参数错误");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        //dmsStoreService.deleteById(id);
        DmsStore dmsStore = new DmsStore();
        dmsStore.setStoreId(id);
        dmsStore.setDeleteTag((byte)1);
        dmsStoreService.update(dmsStore);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "dmsStore", value = "需要修改的内容") @RequestBody DmsStore dmsStore) {
        dmsStoreService.update(dmsStore);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<DmsStore> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        DmsStore dmsStore = dmsStoreService.findById(id);
        return WrapMapper.ok(dmsStore);
    }


    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<DmsStoreDto>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        DmsStoreDto dmsStoreDto = new DmsStoreDto();
        dmsStoreDto.setDeleteTag(false);
        List<DmsStoreDto> list = dmsStoreService.selectStoreByCondition(dmsStoreDto);
        int total = (int) new PageInfo<>(list).getTotal();

        List<DmsStoreDto> dmsStoreDtoList = dmsStoreService.getStoreInfo(list);

        PageInfo pageInfo = new PageInfo(dmsStoreDtoList);
        pageInfo.setTotal(total);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/selectByCondition")
    @ApiOperation(httpMethod = "POST", value = "selectByCondition")
    public Wrapper<PageInfo<DmsStoreDto>> selectlist(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                               @ApiParam(name = "dmsStoreDto", value = "每分页返回的记录数") @RequestBody DmsStoreDto dmsStoreDto ) {
        PageHelper.startPage(page, size);
        System.out.println(dmsStoreDto);
        List<DmsStoreDto> list = dmsStoreService.selectStoreByCondition(dmsStoreDto);
        PageInfo pageInfo = new PageInfo(list);

        List<DmsStoreDto> dmsStoreDtoList = dmsStoreService.getStoreInfo(list);
        pageInfo.setList(dmsStoreDtoList);

        return WrapMapper.ok(pageInfo);
    }

    /**
     * 根据ID获取具体信息
     * @param storeId
     * @return
     */
    @GetMapping("/getStoreInfo")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<DmsStoreDto> getStoreInfo(@ApiParam(name = "storeId", value = "记录storeId") @RequestParam Long storeId) {
        DmsStoreDto dmsStoreDto = dmsStoreService.getStoreInfoById(storeId);
        return WrapMapper.ok(dmsStoreDto);
    }

    /** 司机送货参数scope_id，longitude，latitude
     * 业务员打卡参数storeId，longitude，latitude
     * 判断是否在店铺范围内，storeId，scope_id，longitude，latitude
     * 打不上卡，请业务员重新标定店铺地址
     * @param dmsStoreDto
     * @return
     */
    @PostMapping("/inPosition")
    @ApiOperation(httpMethod = "POST", value = "司机")
    public Wrapper inPosition(@ApiParam(name = "dmsStoreDto", value = "返回相关dmsStoreDto") @RequestBody DmsStoreDto dmsStoreDto ) {
        if (dmsStoreDto == null || dmsStoreDto.getLatitude() == null || dmsStoreDto.getLongitude() == null){
            return WrapMapper.error("参数错误！");
        }
        //追求更准的话，还可以找store_id
        List<DmsStoreDto> list = dmsStoreService.findStorePosition(dmsStoreDto,100);
        if (list != null && list.size()>0){

            return WrapMapper.ok("在该店铺范围");
        }
        return WrapMapper.error("不在该店铺范围");
    }

    /** 司机\业务员当前位置附近店铺
     * @param dmsStoreDto
     * @return
     */
    @PostMapping("/nearStore")
    @ApiOperation(httpMethod = "POST", value = "司机\\业务员当前位置附近店铺")
    public Wrapper<PageInfo<DmsStoreDto>> nearStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "dmsStoreDto", value = "返回相关dmsStoreDto") @RequestBody DmsStoreDto dmsStoreDto ) {
        if (dmsStoreDto == null || dmsStoreDto.getLatitude() == null || dmsStoreDto.getLongitude() == null){
            return WrapMapper.error("参数错误！");
        }
        if (dmsStoreDto.getDistance() == null){
            dmsStoreDto.setDistance(1000);
        }
        PageHelper.startPage(page, size);
        //追求更准的话，还可以找store_id
        List<DmsStoreDto> list = dmsStoreService.findStorePosition(dmsStoreDto,dmsStoreDto.getDistance());

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);

        if (list != null && list.size()>0){


            return WrapMapper.ok(pageInfo);

        }
        return WrapMapper.error("未找到店铺");
    }



    //业务员本月签约店铺数 store_status=1
    @RequestMapping("/partnersStore")
    @ApiOperation(httpMethod = "POST", value = "业务员本月签约店铺数")
    public Wrapper<PageInfo<DmsStoreDto>> partnersStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId ) {

        PageHelper.startPage(page, size);

        List<DmsStoreDto> list = dmsStoreService.partnersStore(userId);

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);

        return WrapMapper.ok(pageInfo);

    }

    //业务员总签约店铺数 store_status=1
    @RequestMapping(value = "/allPartnersStore",method =RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员总签约店铺数")
    public Wrapper<PageInfo<DmsStore>> allPartnersStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId ) {

        PageHelper.startPage(page, size);

        List<DmsStore> list = dmsStoreService.allPartnersStore(userId);

        PageInfo<DmsStore> pageInfo = new PageInfo<>(list);

        return WrapMapper.ok(pageInfo);
    }

    //业务员签约店铺数的搜索
    @RequestMapping(value = "/findPartnersStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员签约店铺数的搜索")
    public Wrapper<PageInfo<DmsStoreDto>> findPartnersStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId ,
            @ApiParam(name = "input", value = "搜索店铺的关键字") @RequestParam String input

    ) {

        PageHelper.startPage(page, size);

        List<DmsStoreDto> list = dmsStoreService.findPartnersStore(userId,input);

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);

        return WrapMapper.ok(pageInfo);
    }


    //全部店铺模糊查寻
    @RequestMapping(value = "/findAllStore",method =RequestMethod.POST )
    @ApiOperation(httpMethod = "POST", value = "全部店铺模糊查寻")
    public Wrapper<PageInfo<DmsStoreDto>> findAllStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "input", value = "搜索店铺的关键字") @RequestParam String input

    ) {
        PageHelper.startPage(page, size);

        List<DmsStoreDto> list = dmsStoreService.findAllStore(input);

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
        return WrapMapper.ok(pageInfo);
    }

    //计算某一个店铺是否在所传的经纬度附近
    @PostMapping("/ifNearStore")
    @ApiOperation(httpMethod = "POST", value = "计算某一个店铺是否在所传的经纬度附近")
    public Wrapper ifNearStore(
        @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
        @ApiParam(name = "dmsStoreDto", value = "返回相关dmsStoreDto") @RequestBody DmsStoreDto dmsStoreDto ) {

        if (dmsStoreDto == null || dmsStoreDto.getLatitude() == null || dmsStoreDto.getLongitude() == null || dmsStoreDto.getStoreId() == null) {
            return WrapMapper.error("参数错误！");
        }

        dmsStoreDto.setDistance(200);

        PageHelper.startPage(page, size);
        //追求更准的话，还可以找store_id

        List<DmsStoreDto> list = dmsStoreService.findStorePosition(dmsStoreDto, dmsStoreDto.getDistance());

        if (list != null && list.size() > 0) {

            for (DmsStoreDto storeDto : list) {
                if (dmsStoreDto.getStoreId().equals(storeDto.getStoreId())) {

                    return WrapMapper.ok(1);
                }
            }
        }
        return WrapMapper.ok(0);
    }

    /**
     * 没有shopId即未注册店铺的store 找到注册过的
     */
    @RequestMapping(value = "/noShopIdStore",method =RequestMethod.POST )
    @ApiOperation(httpMethod = "POST", value = "没有shopId即未注册店铺的store")
    public Wrapper<PageInfo<DmsStoreDto>> findShopIdStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size

    ) {

        PageHelper.startPage(page, size);

        List<DmsStoreDto> list = dmsStoreService.findShopIdStore();

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);

        System.out.println(pageInfo);

        return WrapMapper.ok(pageInfo);
    }



    /**
     * 校验telephone唯一性.
     *
     * @param  @PostMapping(value = "/checkDmsStore")
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkDmsStore")
    @ApiOperation(httpMethod = "POST", value = "校验DmsStore唯一性")
    public Wrapper<Integer> checkDmsStore(@RequestBody DmsStore dmsStore) {

        logger.info("校验手机号唯一性 checkDmsStoreDto={}", dmsStore);

        String telephone = dmsStore.getTelephone();

        String storeName = dmsStore.getStoreName();

        Example example = new Example(DmsStore.class);

        Example.Criteria criteria = example.createCriteria();

        if (dmsStore.getTelephone()!=null&&dmsStore.getTelephone().length()>=0)
        {

            criteria.andEqualTo("telephone", telephone);

            int result = dmsStoreService.selectCountByExample(example);

            if (result>0) {
                return WrapMapper.ok(0);
            }

            return WrapMapper.ok(1);

        }

        if (dmsStore.getStoreName()!=null&&dmsStore.getStoreName().length()>=0){

            criteria.andEqualTo("storeName", storeName);

            int result = dmsStoreService.selectCountByExample(example);

            if (result>0) {
                return WrapMapper.ok(0);
            }return WrapMapper.ok(1);
        }
        return WrapMapper.error("参数错误");
    }

    /**
    *本月新增合作店铺
    */
    @RequestMapping(value = "/partnersStoreByMonth",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "本月新增合作店铺")
    public Wrapper<PageInfo<DmsStoreDto>> partnersStoreByMonth(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId
    ) {

        PageHelper.startPage(page, size);

        List<DmsStoreDto> list = dmsStoreService.joinStoreByMonth(userId);

        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);

        return WrapMapper.ok(pageInfo);
    }

    /**
     * 验证该用户是否是该店铺的服务人
     */
    @GetMapping("/ifServiceStore")
    @ApiOperation(httpMethod = "POST", value = "验证该用户是否是该店铺的服务人")
    public Wrapper ifServiceStore(
            @ApiParam(name = "userId", value = "业务员Id")  @RequestParam Long userId,
            @ApiParam(name = "storeId", value = "店铺Id")  @RequestParam Long storeId
    ) {
       Integer flag = dmsStoreService.ifServiceStore(userId,storeId);
       return WrapMapper.ok(flag);
    }

    /**
     * 退出服务
     */
    @GetMapping("/quitServiceStore")
    @ApiOperation(httpMethod = "POST", value = "退出服务 ")
    public Wrapper quitServiceStore(
            @ApiParam(name = "storeId", value = "店铺Id")  @RequestParam Long storeId
    ) {
        Integer flag = dmsStoreService.quitServiceStore(storeId);
        return WrapMapper.ok(flag);
    }

    /**
     * 退出跟进
     */
    @GetMapping("/quitDevStore")
    @ApiOperation(httpMethod = "POST", value = "退出跟进 ")
    public Wrapper quitDevStore(
            @ApiParam(name = "storeId", value = "店铺Id")  @RequestParam Long storeId
    ) {
        Integer flag = dmsStoreService.quitDevStore(storeId);
        return WrapMapper.ok(flag);
    }

    /*
    业务员待服务搜索
    */
    @RequestMapping(value = "/findPublicServiceStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员待服务")
    public Wrapper<PageInfo<DmsStore>> findPublicServiceStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "搜索", value = "搜索", defaultValue = "0") @RequestParam(defaultValue = "") String input

    )
    {
        if (input.length()== 0 || ("").equals(input)){
            return WrapMapper.ok();
        }
        PageHelper.startPage(page, size);
        List<DmsStore> list = dmsStoreService.findPublicServiceStore(input);
        PageInfo<DmsStore> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    /*
    业务员待跟进搜索
    */
    @RequestMapping(value = "/findPublicDevStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员待服务")
    public Wrapper<PageInfo<DmsStore>> findPublicDevStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "搜索", value = "搜索", defaultValue = "0") @RequestParam(defaultValue = "") String input

    )
    {
        if (input.length()== 0 || ("").equals(input)){
            return WrapMapper.ok();
        }
        PageHelper.startPage(page, size);
        List<DmsStore> list = dmsStoreService.findNeedServiceShop(input);
        PageInfo<DmsStore> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }
}




